package com.mapzone.showmap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MapView extends View {
	
	public native void init_ndk( int pixelx, int pixely, float dpix, float dpiy );
	public native void init_mem_bitmap( Bitmap bitmap, int index );
	public native boolean open_map_doc( String map_path );
	public native void zoom_out(  );
	public native void zoom_in(  );
	public native void region_zoom(  );
	public native void scale_zoom( int scale );//这里传递的参数是缩放的倍数。
	public native void pan( );
	public native void pan( float x, float y);
	public native void select( );
	public native void clear_selection( );
	public native void entire();
	public native String[] get_object_att();
	
	public native void on_touch_down( float x, float y );
	public native void on_touch_move( float x, float y );
	public native void on_touch_up( float x, float y );
	public native void on_touch_double_click( float x, float y );
	public native void display_draw( Bitmap screan_bitmap, float dpix, float dpiy  );
	public native void draw_test_bitmap( Bitmap bitmap );
	
	public native String[] get_layers();
	public native String[] get_fields( int layer_index );
	public native String[] get_values( int layer_index, int field_index );
	public native int query_bound( int value_index );
	
	public native int locate( double x, double y );
	public native int init_gps();
	public native int position_change( );
	
	public native void on_destroy();
	
	
	private int i_count = 0;	
	private boolean b_init = true;
	int i = 0;
	final Handler mHandler = new Handler();
	
	private float dpix, dpiy;
	MapActivity activity;
	private double latitude = 0;//纬度
	private double longtitude = 0;//经度
	private double accuracy = 0;//精度
	private double altitude = 0;//高度
	private double time = 0;//
	private double speed = 0;//
	private double bearing = 0;//
	
	Bitmap screen;
	private int width;
	private int height;
	public boolean b_invalidate = false;
	
	//	
//	private LocationManager location_manager;
	public void init( float x, float y )
	{
		dpix = x;
		dpiy = y;
		Context context = getContext();
		if( context instanceof MapActivity )
		{
			activity = ( MapActivity )context;
		}
	}
	
	public void init_bitmap( int width, int height )
	{
		this.width = width;
		this.height = height;
		for( int i = 0; i < 6; i++ )
		{
			Log.v("", "index = " + i + " width = " + width + " height = " + height );
//			mem_bitmap[i] = Bitmap.createBitmap( width, height, Config.ARGB_8888 ); 
//			init_mem_bitmap( mem_bitmap[i], i );
			Log.v("", "init_mem_bitmap");
		}
	}
	
	public void set_location(  double latitude,//纬度
	double longtitude,//经度
	double accuracy,//精度
	double altitude,//高度
	double time,
	double speed,
	double bearing )
	{
		this.latitude = latitude;
		this.longtitude = longtitude;
		this.accuracy = accuracy;
		this.altitude = altitude;
		this.time = time;
		this.speed = speed;
		this.bearing = bearing;
	}
//
	final Runnable mUpdateResults = new Runnable() {
		public void run() {
//			if( null == screen )
			{
				screen = Bitmap.createBitmap( width, height, Config.ARGB_8888 ); 
			}
			display_draw( screen, dpix, dpiy );
			b_invalidate = true;
			invalidate(); //更新视图
			Log.v(  "SHOWMAP", "in Runnable count = " + i_count++  );
		}
	};
	
	public void Invalidate( int flag )
	{
//		Log.v("SHOWMAP-JAVA", "into Invalidate  flag = " + flag );
//		if( 0 == flag )
//		{
//			invalidate();
//		}
//		else
//		{
		mHandler.post(mUpdateResults);
//		}
	}
	
	boolean b_draw_select = true;
	public void Invalidate_select( )
	{
		b_draw_select = true;
		invalidate();
	}
//	public double get_latitude()
//	{
//		return 0;
//	}
//	
//	public double get_longitude()
//	{
//		return 0;
//	}
	
	public boolean callback_open()
	{
		return activity.open_gps();
	}
	public boolean callback_close()
	{
		return false;
	}
	public boolean callback_is_open()
	{
		return activity.gps_is_open();
	}
	public float callback_speed()
	{
		return (float)speed;
	}
	public int callback_status()
	{
		return 0;
	}
	public double callback_latitude()
	{
		return latitude;
	}
	public double callback_longtitude()
	{
		return longtitude;
	}
	public double callback_altitude()
	{
		return altitude;
	}
	public float callback_heading()
	{
		return ( float )bearing;
	}
	public float callback_position()
	{
		return ( float )accuracy;
	}
	public float callback_horizontal()
	{
		return 0;
	}
	public float callback_vertical()
	{
		return 0;
	}
	
	static
	{
		System.loadLibrary( "mz_sqlite" );
		System.loadLibrary( "foundation" );
		System.loadLibrary( "mathlib" );
		System.loadLibrary( "spatialreference" );
		System.loadLibrary( "geometry" );
		System.loadLibrary( "spatialdatabase" );
		System.loadLibrary( "gdl" );
		System.loadLibrary( "shared" );
		System.loadLibrary( "display" );
		System.loadLibrary( "symbol" );
		System.loadLibrary( "curl" );
		System.loadLibrary( "location" );
		System.loadLibrary( "carto" );
		System.loadLibrary( "undo" );
		System.loadLibrary( "oui" );
		System.loadLibrary( "mapzone_ndk" );
		
		System.loadLibrary( "test_jni" );
	}

	public MapView(Context context) {
		super(context);
	}
	public MapView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
	}
	public MapView(Context context, AttributeSet attrs) 
	{
		super(context, attrs);
	}
	@Override
	public void onDraw(Canvas canvas) 
	{
		super.onDraw(canvas); 
//		canvas.drawLine( 0, 0, 400, 400, paint );
		
//		Bitmap bmp = Bitmap.createBitmap( canvas.getWidth(), canvas.getHeight(), Config.ARGB_8888 );
//		Log.v( "SHOWMAP-JAVA", "before display_draw");
//		draw_test_bitmap( bmp );
//		Log.v( "SHOWMAP-JAVA", "after display_draw");
//        canvas.drawBitmap( bmp, 0, 0, paint ); 
        
		final Paint paint = extracted();  
        if( b_invalidate )
        {
        	Log.v("",  "canvas.drawbitmap" );
        	canvas.drawBitmap( screen, 0, 0, paint );
			b_invalidate = false;
        }

	}
	private Paint extracted() {
		return new Paint();
	}
	
}
